package com.supreme.studyserver.controller;

import com.supreme.commonboot.utils.AjaxResp;
import com.supreme.studyserver.service.TaskScheduleService;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

@RestController
@RequestMapping(value = "task")
public class TaskScheduleController {

    @Resource
    private TaskScheduleService taskScheduleService;

    @GetMapping(value = "asyncFunc")
    public AjaxResp demoAsyncFunction() {
        taskScheduleService.demoAsyncFunction();
        return AjaxResp.instance();
    }

    @GetMapping(value = "asyncFutureFunc")
    public AjaxResp demoAsyncFuture() throws ExecutionException, InterruptedException {
        final Future<String> asyncFuture1 = taskScheduleService.demoAsyncFuture();
        final String result1 = asyncFuture1.get();
        final Future<String> asyncFuture2 = taskScheduleService.demoAsyncFuture();
        final String result2 = asyncFuture2.get();
        return AjaxResp.instance(new String[]{result1, result2});
    }

    @GetMapping(value = "asyncListenableFutureFunc")
    public AjaxResp demoAsyncListenableFuture() throws ExecutionException, InterruptedException {
        final ListenableFuture<Integer> integerListenableFuture = taskScheduleService.demoAsyncListenableFuture();
        final Integer result = integerListenableFuture.get();
        return AjaxResp.instance(result);
    }
}
